package com.qf.business.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.business.user.dao.StudentDao;
import com.qf.business.user.service.StudentService;
import com.qf.commons.web.aspect.annotation.GetUser;
import com.qf.commons.web.aspect.utils.UserUtils;
import com.qf.data.user.entity.Student;
import com.qf.data.user.vo.input.StuLoginVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 学生信息表(Student)表服务实现类
 *
 * @author makejava
 * @since 2022-12-02 17:02:08
 */
@Service("studentService")
public class StudentServiceImpl extends ServiceImpl<StudentDao, Student> implements StudentService {

    // 请求1 - 小明
    // 请求2 - 小明
    @Override
    public Student queryStu(StuLoginVo stuLoginVo) {
        //根据openid查询学生信息
        Student student = this.query().eq("openid", stuLoginVo.getOpenid()).one();
        if (student == null) {
            //进行注册
            student = new Student();
            BeanUtils.copyProperties(stuLoginVo, student);
            //保存到数据库
            this.save(student);
        }
        return student;

//        if (student == null) {
//            synchronized (stuLoginVo.getOpenid().intern()) {
//                student = this.query().eq("openid", stuLoginVo.getOpenid()).one();
//                if (student == null) {
//                    //进行注册
//                    student = new Student();
//                    BeanUtils.copyProperties(stuLoginVo, student);
//
//                    //保存到数据库
//                    this.save(student);
//                }
//            }
//        }
//        return student;
    }

    @Override
    @GetUser
    public int updateUserScore(Integer score, Integer action) {
        Student stu = UserUtils.getUser(Student.class);
        return getBaseMapper().updateUserScore(stu.getId(), score, action);
    }

    @Override
    public int updateUserScore(Integer score, Long uid, Integer action) {
        return getBaseMapper().updateUserScore(uid, score, action);
    }
}

